class ObjectPool {
	public pool = new Object();
	public creator = new Object();
	public effectPool = new Object();
	public bulletPool = new Object();

	public hpTextFieldPool:Array<egret.TextField> = new Array<egret.TextField>();
	public constructor() {
		this.registCreator("tip", function() {
			var textField = new egret.TextField();
			return textField;
		});
	}

	public getHpTextField():any {
		if (this.hpTextFieldPool.length > 0) {
			var obj = this.hpTextFieldPool.pop();
			return obj;
		}
		var textField:egret.TextField = new egret.TextField();
		textField.textColor = 0xff0000;
		textField.bold = true;
		return textField;		
	}

	public releaseHpTextField(obj:egret.TextField):any {
		this.hpTextFieldPool.push(obj);
	}

	


	public getEffect(key:string, compFunc:Function ,thisObject:any) {
		var arr:Array<Object> = this.effectPool[key];
		if (!arr) {
			arr = new Array<Object> ();
			this.effectPool[key] = arr; 
		}
		//console.log("申请特效[", key, "]");
		if (arr.length > 0) {
			console.log("特效池[", key, "]长度" + arr.length);
			var obj = arr.pop();
			compFunc.call(thisObject, obj);
			return;
		}
		GameMain.me.resource.getEffect(key, function(data:egret.MovieClipData){
			var mc = new egret.MovieClip(data);
            compFunc.call(thisObject, mc);
		}, this);
	}

	public releaseEffect(key:string, mc:egret.MovieClip) {
		if (!this.effectPool[key]) {
			return;
		}
		//console.error("释放特效[", key, "]");
		var arr:Array<Object> = this.effectPool[key];
		arr.push(mc);
	}


	public registCreator(key:string, creator:Function) {
		this.creator[key] = creator;		
	}	


	public getObject(key:string) {
		var arr:Array<Object> = this.pool[key];
		if (!arr) {
			arr = new Array<Object> ();
			this.pool[key] = arr; 
		}
		if (arr.length > 0) {
			console.log("对象池[", key, "]长度" + arr.length);
			var obj = arr.pop();
			return obj;
		}
		var creator = this.creator[key];
		if (!creator) {
			return null;
		}
		return creator();
	}

	public releaseObject(key:string, obj:Object) {
		if (!this.pool[key]) {
			return;
		}
		var arr:Array<Object> = this.pool[key];
		arr.push(obj);
	}
}